﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using DiscoveryLogic.Analytics.DataObjects.Rules;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.DataObjects.Dedup
{
    public class NetworkDedupSetting
    {
        private string _NetworkName;
        /// <summary></summary>
        public string NetworkName
        {
            get
            {
                return this._NetworkName;
            }
            set
            {
                this._NetworkName = value;
            }
        }

        private Dictionary<EntityType, string> _DedupRules;
        /// <summary>
        /// entity type-> rule name
        /// </summary>
        public Dictionary<EntityType, string> DedupRules
        {
            get
            {
                return this._DedupRules;
            }
            set
            {
                this._DedupRules = value;
            }
        }

        public void Save()
        {
            string filePath = Path.Combine(DataFolder.NetworkDataFolder,
                                           this.NetworkName + DataFolder.NetworkDedupFileExtension);
            if(File.Exists(filePath))
                File.Delete(filePath);
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml("<NetworkDedup></NetworkDedup>");
            XmlNode root = xDoc.DocumentElement;
            XmlDataUtil.UpdateAttribute(ref xDoc, root, "NetworkName",this.NetworkName);
            if(this.DedupRules !=null && this.DedupRules.Count>0)
            {
                foreach(EntityType entityType in this.DedupRules.Keys)
                {
                    XmlNode dedupNode = XmlDataUtil.AddElement(ref xDoc, root, "DedupRule");
                    XmlDataUtil.UpdateAttribute(ref xDoc, dedupNode, "EntityType",entityType.ToString());
                    XmlDataUtil.UpdateAttribute(ref xDoc, dedupNode,"RuleName", this.DedupRules[entityType]);
                }
            }
            xDoc.Save(filePath);
        }

        public static NetworkDedupSetting Read(string networkName)
        {
            NetworkDedupSetting dedupSetting = new NetworkDedupSetting();
            string filePath = Path.Combine(DataFolder.NetworkDataFolder,
                                           networkName + DataFolder.NetworkDedupFileExtension);
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filePath);
            XmlNode root = xDoc.DocumentElement;
            dedupSetting.NetworkName = XmlDataUtil.GetAttributeValue(root, "NetworkName", "");
            dedupSetting.DedupRules = new Dictionary<EntityType, string>();
            XmlNodeList dedupNodes = root.SelectNodes("DedupRule");
            if(dedupNodes !=null && dedupNodes.Count>0)
            {
                foreach(XmlNode dedupNode in dedupNodes)
                {
                    EntityType entityType =
                        (EntityType) Enum.Parse(typeof (EntityType),
                                                XmlDataUtil.GetAttributeValue(dedupNode, "EntityType",
                                                                              EntityType.Grants.ToString()));
                    string ruleName = XmlDataUtil.GetAttributeValue(dedupNode, "RuleName", "");
                    dedupSetting.DedupRules.Add(entityType,ruleName);
                }
            }
            return dedupSetting;
        }

        public static bool Exists(string networkName)
        {
            string filePath = Path.Combine(DataFolder.NetworkDataFolder,
                                           networkName + DataFolder.NetworkDedupFileExtension);
            if(File.Exists(filePath))
                return true;
            else
                return false;
        }
    }
}
